//<script type='text/javascript'>


/////// LIST CLASS
var List = function (vars) {
	Object.extend( lst = engine.generate_unique_element( "list" ), win.asset.list);
	lst.vars = vars;

	if( vars.blank ) lst.blank( vars.blank, vars.ext );
	if( vars.descr ) lst.insert( new Element( "span", { 'class':"name" }).update( vars.descr ));
	lst.insert( lst.ul = new Element( "ul", { 'class': "border" }));
	
	lst.observe( "mouseover", controls.bubble );
	
	return ( vars.floats )? lst.seed( vars.floats, vars.flow ) : lst;
}

win.asset.list	= {
	objtype	: "list",
	seed	: function( floats, flow, reqs ) {
		
		this.vars.floats	= floats;
		this.vars.flow 		= flow;
		if( reqs )
			this.vars.reqs 	= reqs;
		
		if( flow ) this.ul.update();
		
		floats.each( function( el ) { li = this.ul.insert( new Li( el, this.vars.ext )); }.bind( this ));
		
		
		
		if( $( 'control_list' )) $( 'control_list' )._position( this );

		//this.sortable.delay( 0.2, this.vars.type );		
		return this;
    },
    catch_ext	: function() {
    
    	engine.ext.newlistitem( this );
    }
}
Object.extend(win.asset.list, win.asset._proto_);


/////// DROPBOX CLASS
var Dropbox = function (vars) {
	
	// Create element
	Object.extend( obj = engine.generate_unique_element( "dropbox" ) , win.asset.dropbox );
	obj.vars = vars;
	
	// Add button required?
	if( vars.blank ) obj.blank( vars.blank );
	else if( vars.new_media ) obj.new_media( vars.new_media );
	
	// Add child elements
	if( vars.descr ) obj.insert( new Element( "span", { "class": "name" } ).update( vars.descr ));
	obj.insert( obj.box = new Element( "ul", { "id": obj.id + ".box", "class": "box" } ));

	//obj.observe("mouseover", controls.bubble );

	return ( vars.floats )? obj.seed( vars.floats ) : obj;
}

win.asset.dropbox	= {
	all			: {},
	seed		: function( floats, flow ) {

        floats.each( function(el) { this.box.insert( new Float( el )); }, this );

		this.sortable.delay( 0.2, this.vars.type )
		
		return this;
		
    },
    check		: function( e ){
		
		am_stored  = ( this.vars.floats )? this.vars.floats.length : 0;
		am_current = this.box.childElements().length;
        
		///// PARSE NEW ORDER
		new_floats = [];
		this.box.childElements().each( function( child ){
			new_floats.push( child.vars );
		});
		
		var params = { floats: new_floats };

		///// CALCULATE CHANGES
		if		( am_current > am_stored ) params.add		= new_floats.findAll( function( value ) { return (this.vars.floats)? !this.vars.floats.include( value ) : false; }.bind( this ));
		else if	( am_current < am_stored ) params.remove	= this.vars.floats.findAll( function( value ) { return !new_floats.include( value ); }.bind( this ));
		
		Object.extend( this.vars, params );
		dashboard.user.logtime( init.session );
		
		
		///// IF CHILD-AMOUNT IS DISPLAYED
		if( this.up(0).hasClassName( "drop" ))
			this.up(0).inc.setStyle({ backgroundPosition: ( this.vars.floats.length )? (this.vars.floats.length*10 -88) + "px" : "-92px" });


		return this.request( "sortable" );
	},
    
    unset		: function() {
		delete win.asset.dropboxMK2.all[ this.id + ".box" ];
		this.sortable( false );
    },
    
    add			: function(e) {
        vars = win.asset.dropbox.list[this.parentNode.id];
        xhttp.send('control.call.php', {trg:this.parentNode.id, wtype:'push', tool:vars.tool, action:'add', 'vars':vars});
    },
    new_media	: function(vars) {
    	el = this.blank();
    	el.update( new Element("span", {id:el.id+".trg"}).update('...connecting') );
    	
    	Object.extend(loadcluster.init, {
			button_placeholder_id: el.id+".trg",
			post_params : { account :'<?=ACCOUNT?>', auth:<?=$Engine->user( 'ID' )?>, objtype : vars.type, active : 0 },
			custom_settings : { float_target : this.id, objtype : vars.type }
		} );
		
		setTimeout( function() { var swfu = new SWFUpload( loadcluster.init ); }, 200 );
	},
	
	sortable	: function( type ) {
		
		related = [];
		
		$$( "ul.box" ).each( function( box ) {
			if( box.up(0).vars.type == type ) related.push( box.id );
		}).each( function( box ) {
			if( box.up(0).vars.type == type )
				Sortable.create( box.id , { dropOnEmpty:true, ghosting:false, constraint:false, containment: related, onUpdate: box.up(0).check.bind( box.up(0) ) });
		});
		
		return this;
    }
}
Object.extend(win.asset.dropbox, win.asset._proto_);


/////// OPTIONS CLASS
var Pairs = function ( vars ) {
	Object.extend(obj = engine.generate_unique_element( "pairs" ), win.asset.pairs );
	obj.vars = vars;
	
	if( vars.blank ) obj.blank( vars.blank );
	if( vars.descr ) obj.insert( new Element( "span", { 'class':"name" }).update( vars.descr ));
	obj.insert( obj.ul = new Element( "ul", { 'class': "border" }));
	
	return ( vars.floats )? obj.seed( vars.floats ) : obj;
}

win.asset.pairs	= {
	seed	: function( floats ) {

		floats.each( function( el ) { li = this.ul.insert( new Pair( el )); }, this );
	
		return this;
	},
	blank	: function( string ) {
		this.insert( div = new Element("div", { id: this.id + ".blank", 'class':"blank" }).update( string ));
		div.observe( "click", function() {
			this.ul.insert( new Pair( ['',''], this.vars ));
		}.bind( this ));

		return div;
	},
	update	: function() {
		
		obj = [];
		this.select( "li" ).each( function( el, i ){ 
			obj[i] = [];
			el.select( "input" ).each( function( el, j ){  obj[i][j] = el.value; } );
		} );
		
		this.vars.value = obj;//Object.toJSON( obj );
		xhttp.send( 'control.call.php', { transfer_type:'push', type: this.vars.type, action:"ajax", vars: this.vars });
		
	}
}
//Object.extend(win.asset.pairs, win.asset._proto_);

/////// OPTIONS CLASS
var Options = function ( vars ) {
	Object.extend(obj = engine.generate_unique_element( "options" ), win.asset.options);
	obj.vars = vars;
	
	if( vars.active ) obj.addClassName( "active" );
	
	obj.insert(
		new Element( "div", { 'class': "name" }).update( vars.descr || "..." ).observe( 'click', obj.toggleClassName.bind( obj, "active" ))
	).insert( obj.ul = new Element( "ul" ));
	
	return ( vars.floats )? obj.seed( vars.floats ) : obj;
}

win.asset.options	= {
	seed	: function( floats ) {

		floats.each( function( child ) {
            this.ul.insert( new Element( "li" ).update( child ));
        }, this );        
	
		return this;
	}

}
Object.extend(win.asset.options, win.asset._proto_);


/*
sexy code example:
this.eventList = this.eventList || {};
*/
